home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10715 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  61 lines

  1. Path: maple.sover.net!mountain
  2. From: mountain@maple.sover.net (Steve Mount)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Converting Strings to Upper Case
  5. Date: 19 Mar 1996 16:44:19 GMT
  6. Organization: SoVerNet, Inc.
  7. Message-ID: <4imo93$mu@thrush.sover.net>
  8. References: <4ifra6$52i@scipio.cyberstore.ca> <4ih7l3$526@thrush.sover.net> <314D8C90.55C6@oak.westol.com>
  9. NNTP-Posting-Host: maple.sover.net
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Mark Kintigh (breetai@oak.westol.com) wrote:
  13. : Steve Mount wrote:
  14. : > 
  15. : > In article <4ifra6$52i@scipio.cyberstore.ca>, ejw@news.cyberstore.ca says...
  16. : > >I need to write a function to convert a string containg upper or lower case
  17. : > >characters to the opposite case.  Something like::
  18. : > void strnlwr(char *buffer, int len)
  19. : > {
  20. : > register int i;
  21. : > for (i=0;i<len;i++) buffer[i] = tolower(buffer[i]);
  22. : > return;
  23. : > }
  24. : > 
  25. : > Note I added a length checker, as this was required; it may not be the most
  26. : > efficient, but it gets the job done.  It also prevents me from duplicating
  27. : > the code that tolower/toupper does.
  28.  
  29. : You don't need the length.  You could just use....
  30.  
  31. : void str2upper(char *str)
  32. : {
  33. :   while(*str!='\0')
  34. :   {
  35. :     *str = toupper(*str);
  36. :     str++;
  37. :   }
  38. : }
  39.  
  40. : Of course, this assumes that you have properly terminated the string. :)
  41.  
  42. Much of our data is similar to this:
  43. struct {
  44.   char firstname[12];
  45.   char lastname[12];
  46.   ...etc...
  47.   };
  48. with no null-termination.  So almost all of our in-house functions take
  49. a length.  The function I posted follows this "standard" of ours.  If
  50. you can guarantee null-temrination, then it is a lot simpler, and 
  51. probably quicker, as your function shows.  But we can't guarantee 
  52. null-termination.  In fact, most of the time we can guarantee
  53. non-null-termination!
  54.  
  55. +============================================================================+
  56. | Steve Mount, Software Engineer            Work: sjjm@hawkeye.idx.com       |
  57. | CIS: 73720,3404  MSN: S_Mountain          Home: mountain@sover.net         |
  58. | AOL: Mountain                                                              |
  59. | WWW: http://www.sover.net/~mountain/      "Fight, Win, Prevail!"           |
  60. +============================================================================+
  61.